#include "LeagueHistoryMenu.h"
#include "League.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "ServiceProvider.h"
#include "LeagueManager.h"
#include "TeamManager.h"
#include "MenuManager.h"
#include "LeagueMenu.h"
#include "Types.h"

LeagueHistoryMenu::LeagueHistoryMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
	, m_leagueID(Utilities::INVALID_ID)
{
}

void LeagueHistoryMenu::Draw() const
{
	using STD::cout;
	using STD::endl;

	const League& league = *GetSP()->GetLeagueMgr().GetLeagueFromID(m_leagueID);

	cout << Print::Title("LEAGUES HISTORY:");
	cout << "LEAGUE " <<league.GetName() << endl;

	ShowLeagueHistory();

	STL::string options = "1-N) Select year N in league.\n\n0) Return to previous menu'.";
	cout << Print::Options(options) << "Selection: ";
}

void LeagueHistoryMenu::ShowLeagueHistory() const
{
	using namespace Utilities;
	using STD::cout;
	using STD::endl;

	const League& league = *GetSP()->GetLeagueMgr().GetLeagueFromID(m_leagueID);
	const LeagueItemHistoryVec& historyItems = league.GetHistory().GetItems();

	const size_t max_char_team_name = Team::GetParams().MAX_CHAR_TEAM_NAME + 1;

	if(historyItems.empty())
	{			
		Print::PrintDecorationLine(cout, '-');
		cout << endl;

		cout << TabUpTo("No results",10) << endl;

		Print::PrintDecorationLine(cout, '-');
		cout << endl;
	}
	else
	{
		cout << TabUpTo("IDX",5);
		cout << TabUpTo("YEAR", 7);
		cout << TabUpTo("WINNER TEAM", max_char_team_name);
		cout << TabUpTo("PTS", 6);
		cout << TabUpTo("SECOND TEAM", max_char_team_name);
		cout << TabUpTo("PTS", 6);
		cout << TabUpTo("THIRD TEAM", max_char_team_name);
		cout << TabUpTo("PTS", 6);
		cout << endl;
		Print::PrintDecorationLine(cout, '-');
		cout << endl;

		for (size_t i = 0; i < historyItems.size(); ++i)
		{
			cout << TabUpTo(i+1, 5);
			cout << TabUpTo(historyItems[i].m_year, 7);
			cout << TabUpTo(GetSP()->GetTeamMgr().GetTeamName(historyItems[i].m_result[0].m_teamID), max_char_team_name);
			cout << TabUpTo(historyItems[i].m_result[0].m_score, 6);
			cout << TabUpTo(GetSP()->GetTeamMgr().GetTeamName(historyItems[i].m_result[1].m_teamID), max_char_team_name);
			cout << TabUpTo(historyItems[i].m_result[1].m_score, 6);
			cout << TabUpTo(GetSP()->GetTeamMgr().GetTeamName(historyItems[i].m_result[2].m_teamID), max_char_team_name);
			cout << TabUpTo(historyItems[i].m_result[2].m_score, 6);
			cout << endl;
		}
	}

	cout << endl;
}

bool LeagueHistoryMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;

	const League& league = *GetSP()->GetLeagueMgr().GetLeagueFromID(m_leagueID);
	const LeagueItemHistoryVec& historyItems = league.GetHistory().GetItems();

	size_t res = Utilities::StringToInt(i_input);

	if (res == 0)
	{
		GetMM()->SetMenu(GetMM()->GetLeagueStatsMenu());
	}
	else if (res <= historyItems.size())
	{
		LeagueMenu* nextMenu = static_cast<LeagueMenu*>(GetMM()->GetLeagueMenu());
		nextMenu->SetHistoryItemStat(&historyItems.at(res-1), league.GetName());
		GetMM()->PushMenu(nextMenu);
	}
	else
	{
		goodInput = false;
	}

	return goodInput;
}